package com.example.ems.servlet;

import com.example.ems.bean.Department;
import com.example.ems.bean.Employee; // 引入 Employee bean
import com.example.ems.dao.DepartmentDAO;
import com.example.ems.dao.EmployeeDAO; // 引入 EmployeeDAO
// import jakarta.servlet.ServletException; // 改为 javax
// import jakarta.servlet.annotation.WebServlet; // 改为 javax
// import jakarta.servlet.http.HttpServlet; // 改为 javax
// import jakarta.servlet.http.HttpServletRequest; // 改为 javax
// import jakarta.servlet.http.HttpServletResponse; // 改为 javax
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.List;

@WebServlet("/DepartmentServlet")
public class DepartmentServlet extends HttpServlet {
    private DepartmentDAO departmentDAO;
    private EmployeeDAO employeeDAO; // 用于获取员工列表作为负责人选项

    public void init() {
        departmentDAO = new DepartmentDAO();
        employeeDAO = new EmployeeDAO(); // 初始化 EmployeeDAO
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // request.setCharacterEncoding("UTF-8"); // 应该由 EncodingFilter 处理

        String action = request.getParameter("action");
        String idStr = request.getParameter("id");
        String name = request.getParameter("name");
        String managerIdStr = request.getParameter("managerId"); // 获取负责人ID

        Department department = new Department();
        if (idStr != null && !idStr.isEmpty()) {
            department.setId(Integer.parseInt(idStr));
        }
        department.setName(name);

        if (managerIdStr != null && !managerIdStr.isEmpty() && !managerIdStr.equals("0")) { // "0" 通常表示未选择
            department.setManagerId(Integer.parseInt(managerIdStr));
        } else {
            department.setManagerId(null); // 如果未选择或选择 "0"，则设为null
        }

        if ("add".equals(action)) {
            departmentDAO.addDepartment(department);
        } else if ("update".equals(action)) {
            departmentDAO.updateDepartment(department);
        }
        response.sendRedirect(request.getContextPath() + "/DepartmentServlet?action=list");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String action = request.getParameter("action");
        String searchName = request.getParameter("searchName"); // 获取查询参数
        request.setAttribute("searchNameVal", searchName); // 回传给JSP，用于保留搜索框内容

        if (action == null) action = "list";

        List<Employee> employeeList = employeeDAO.getAllEmployees();
        request.setAttribute("employeeList", employeeList);

        switch (action) {
            case "new":
                request.getRequestDispatcher("/department_form.jsp").forward(request, response);
                break;
            case "edit":
                // ... (原有 edit 逻辑) ...
                int id = Integer.parseInt(request.getParameter("id"));
                Department existingDept = departmentDAO.getDepartmentById(id);
                request.setAttribute("department", existingDept);
                request.getRequestDispatcher("/department_form.jsp").forward(request, response);
                break;
            case "delete":
                // ... (原有 delete 逻辑) ...
                int deleteId = Integer.parseInt(request.getParameter("id"));
                departmentDAO.deleteDepartment(deleteId);
                response.sendRedirect(request.getContextPath() + "/DepartmentServlet?action=list" + (searchName != null ? "&searchName=" + java.net.URLEncoder.encode(searchName, "UTF-8") : "")); // 删除后保持查询条件
                break;
            case "list":
            default:
                List<Department> listDepartment;
                if (searchName != null && !searchName.trim().isEmpty()) {
                    listDepartment = departmentDAO.getDepartmentsByName(searchName.trim());
                } else {
                    listDepartment = departmentDAO.getAllDepartments();
                }
                request.setAttribute("listDepartment", listDepartment);
                request.getRequestDispatcher("/department_list.jsp").forward(request, response);
                break;
        }
    }
}
